#!env python

# Write a program that given a file containing a list of words (one
# word per line, no spaces, all lower case) identifies the longest
# word in the file that can be constructed by concatenating copies of
# shorter words also found in the file.

import Queue
from threading import Thread
from processing import Process, Queue, Lock

class LongestComposition(Process):
    def __init__( self, wordset, in_queue, out_queue ):
        Process.__init__(self)
        self.in_queue = in_queue
        self.out_queue = out_queue
        self.wordset = wordset

    def composed(self, word):
        for i in range(1,len(word)):
            pre = word[0:i]
            post = word[i:len(word)+1]
            if ( pre in self.wordset and
                 ( post in self.wordset or self.composed(post))):
                return True
        return False

    def run(self):
        result = ""
        while 1:
            (op,word,cresult) = self.in_queue.get()
            if op == 0:
                return
            if ( len(cresult) > len(result) ):
                result = cresult
            length = len(result)
            if ( len(word) > length and 
                 self.composed(word )):
                if ( len(word) > len(result) ):
                    result = word
                    out_queue.put(result)

if __name__ == "__main__":

    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-t", "--threads", dest="threads",
                      type="int",
                      help="threads to use", default=1 )

    (options, args) = parser.parse_args()

    import sys
    words = [ i.strip() for i in sys.stdin ]
    wordset = frozenset(words)
    words = list(wordset)

    out_queue = Queue()
    in_queue = Queue()

    threads = []
    thread_count = options.threads
    for i in range(thread_count):
        lc = LongestComposition( wordset, 
                                 in_queue,
                                 out_queue )
        lc.start()
        threads.append(lc)
        
    result = ""
    for word in words:
        if ( not in_queue.full() ):
            in_queue.put( (1, word, result) )
        try:
            cresult = out_queue.get_nowait()
            if ( len(cresult) > len(result) ):
                result = cresult
        except:
            pass
            
        
    for t in threads:
        in_queue.put((0,"s","s"))
    for t in threads:
        t.join()

    while not out_queue.empty():
        cresult = out_queue.get()
        if ( len(cresult) > len(result) ):
            result = cresult

    print len(result),result
